# -*- coding: utf-8 -*-
import os
import pymongo
from EventMonitor import config

class EventmonitorPipeline(object):
    def __init__(self):
        CUR = '/'.join(os.path.abspath(__file__).split('/')[:-2])
        self.news_path = os.path.join(CUR, 'news')
        if not os.path.exists(self.news_path):
            os.makedirs(self.news_path)
        conn = pymongo.MongoClient(config.MONGODB_IP, config.MONGODB_PORT)
        self.col = conn[config.MONGODB_NAME][config.MONGODB_TABLE]

    '''处理采集资讯, 存储至Mongodb数据库'''
    def process_item(self, item, spider):
        try:
            self.col.insert(dict(item))
        except (pymongo.errors.WriteError, KeyError) as err:
            pass
        return item
